from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

if __name__ == '__main__':
    load_dict = []
    browser = webdriver.Chrome()
    browser.get("http://171.111.158.204:8080/shida/School.aspx")

    username = browser.find_element_by_xpath("//input[@id='txtKSH']")
    password = browser.find_element_by_xpath("//input[@id='txtWDF']")
    username.send_keys('******')
    password.send_keys('******')
    wait = WebDriverWait(browser, 120)
    enter = wait.until(EC.presence_of_element_located((By.ID, 'topControl1_NoticeLinkLabel')))
    for data in load_dict:
        browser.get("http://171.111.158.204:8080/shida/SchoolStuList.aspx")
        search = browser.find_element_by_xpath("//input[@id='KeyWords']")
        search.clear()
        search.send_keys(data['xh'])
        click = browser.find_element_by_xpath("//input[@id='btnSearch']")
        click.click()
        href = browser.find_element_by_xpath("//table[@bgcolor='#c7d4f1']//tr[2]/td[1]/a")
        browser.get(href.get_attribute('href'))
        href = browser.find_element_by_xpath("//ul[@id='leftList']/li[2]/a")
        browser.get(href.get_attribute('href'))
        txtJTTXDZ = browser.find_element_by_xpath("//input[@id='txtJTTXDZ']")
        txtQQ = browser.find_element_by_xpath("//input[@id='txtQQ']")
        txtLXDH = browser.find_element_by_xpath("//input[@id='txtLXDH']")
        txtEMAIL = browser.find_element_by_xpath("//input[@id='txtEMAIL']")
        txtJTTXDZ.clear()
        txtJTTXDZ.send_keys(data['address'])
        txtQQ.clear()
        txtQQ.send_keys(data['qq'])
        txtLXDH.clear()
        txtLXDH.send_keys(data['tel'])
        txtEMAIL.clear()
        txtEMAIL.send_keys(data['email'])
        AddButton = browser.find_element_by_xpath("//input[@id='AddButton']")
        AddButton.click()
        browser.switch_to_alert().accept()

    browser.close()
